package com.tepth.remote.core.net.retrofit;

import com.tepth.remote.core.app.ConfigKeys;
import com.tepth.remote.core.app.Control;
import com.tepth.remote.core.net.CommonParams;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Description:Retrofit创建工具类
 *
 * @author Hequn.Lee
 * @date 2018/11/8
 */
class RetrofitCreator {

    /**
     * Retrofit构造类
     */
    private static final class RetrofitHolder {
        /**
         * 获取主地址
         */
        private static final String BASE_URL = (String) Control.getConfiguration(ConfigKeys.RETROFIT_HOST);
        /**
         * 获得Retrofit连接工具
         */
        private static final Retrofit RETROFIT_CLIENT = new retrofit2.Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(OkHttpHolder.OK_HTTP_CLIENT)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * OKHttp构造对象
     */
    private static final class OkHttpHolder {
        /**
         * 获取OKHttp连接类
         */
        private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder()
                .connectTimeout(CommonParams.HTTP_TIME_OUT, TimeUnit.MILLISECONDS)
                .build();
    }

    /**
     * 获取RxService接口
     */
    private static final class RxRetrofitServiceHolder {
        private static final RxRetrofitService RETROFIT_SERVICE =
                RetrofitHolder.RETROFIT_CLIENT.create(RxRetrofitService.class);
    }

    /**
     * 获取RxService对象
     *
     * @return RxService对象
     */
    static RxRetrofitService getRxRetrofitService() {
        return RxRetrofitServiceHolder.RETROFIT_SERVICE;
    }
}
